//
//  ESTFMDBTool.m
//  ESTSCRMAPP
//
//  Created by 李贤立 on 2018/9/25.
//  Copyright © 2018年 Capfer. All rights reserved.
//

#import "ESTFMDBTool.h"
#import <FMDB.h>
#define Table_Name_Province           @"T_PROVINCE"
#define Table_Name_City               @"T_CITY"
#define Table_Name_Country            @"T_COUNTRY"
#define Table_Name_SysValue           @"T_SYSVALUE"
@interface ESTFMDBTool ()
/**
 *  当前时间
 */
@property(nonatomic, copy) NSString *currentTime;
/**
 *  数据库引用
 */
@property(nonatomic, strong) FMDatabase *dataBase;
/**
 *  数据库操作线程
 */
@property (nonatomic, strong) dispatch_queue_t  dispatchQueue;
@end
@implementation ESTFMDBTool
+ (instancetype)sharedFMDBTool {
    static ESTFMDBTool *instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken,^{
        instance = [[ESTFMDBTool alloc] init];
        [ESTProvinceFMDB sharedFMDBTool];
        [ESTCityFMDB sharedFMDBTool];
        [ESTCountryFMDB sharedFMDBTool];
        [ESTSysValueFMDB sharedFMDBTool];
    });
    return instance;
}
- (FMDatabase *)dataBase {
    if (_dataBase == nil) {
        _dataBase = [FMDatabase databaseWithPath:BaseInfo.sqlite];
    }
    return _dataBase;
}
- (NSString *)currentTime {
    return [[NSDate date] formatterDate:@"yyyy-MM-dd HH:mm"];
}
- (dispatch_queue_t)dispatchQueue {
    if (_dispatchQueue == nil) {
        _dispatchQueue = dispatch_queue_create("FMDBQUEUE", DISPATCH_QUEUE_CONCURRENT);
    }
    return _dispatchQueue;
}
- (void)createDataBaseWithSql:(NSString *)sql tableName:(NSString *)tableName {
    if ([self.dataBase open]) {
        // 建立Browser表
        FMResultSet *rs = [self.dataBase executeQuery:@"select count(*) as 'count' from sqlite_master where type ='table' and name = ?", tableName];
        BOOL isExit = NO;
        while ([rs next]) {
            NSInteger count = [rs intForColumn:@"count"];
            isExit = count;
            break;
        }
        if (isExit) {
            NSLog(@"%@表已经存在", tableName);
        } else {
            if ([self.dataBase executeUpdate:sql]) {
                NSLog(@"%@成功创建表", tableName);
            } else {
                NSLog(@"%@创建表的时候出错", tableName);
            }
        }
        [self.dataBase close];
    } else {
        NSLog(@"打开数据库的时候出错");
    }
}

/**
 写入原始数据
 
 @param dataArray 原始数据
 */
- (void)writeSqlWithDataArray:(NSArray<NSDictionary *> *)dataArray {
    dispatch_async(self.dispatchQueue, ^{
        dispatch_semaphore_t semaphore = dispatch_semaphore_create(1);
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
        if ([dataArray isKindOfClass:[NSArray class]]) {
            [self delete];
            [dataArray enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull parameter, NSUInteger idx, BOOL * _Nonnull stop) {
                if ([self.dataBase open]) {
                    [self writeOneselfDataWithParameter:parameter];
                    [self.dataBase close];
                }
            }];
        }
        dispatch_semaphore_signal(semaphore);
    });
}
- (void)writeOneselfDataWithParameter:(NSDictionary *)parameter {
    
}
/**
 插入多条数据

 @param modelArray 数据模型
 */
- (void)insertWithArray:(NSArray *)modelArray {
    
}

/**
 插入一条数据(插入前先查询，如果有则删除再插入)

 @param model 数据模型
 */
- (void)insertWithModel:(id)model {
    
}

/**
 删除所有数据
 */
- (void)delete {
    
}

/**
 删除具体的ID的数据

 @param mainId 识别的ID
 */
- (void)deleteWithMainId:(NSString *)mainId {
    
}

/**
 根据模型更新数据

 @param model 数据模型
 */
- (void)updateDataWithModel:(id)model {
    
}

/**
 根据识别ID查询整个模型

 @param mainId 识别ID
 @return 数据模型
 */
- (id)queryWithMainId:(NSString *)mainId {
    return nil;
}

/**
 查询整个表

 @return 模型数组
 */
- (NSArray *)query {
    return nil;
}

/**
 查询有效的最后修改的时间

 @param licenseId licenseId
 @return 最后修改的时间
 */
- (NSString *)lastModifyWithLicenseId:(NSString *)licenseId {
    return @"";
}
@end

@interface ESTProvinceFMDB ()
/**
 *  表
 */
@property(nonatomic, copy) NSString *table_name;
/**
 *  表头项目
 */
@property(nonatomic, strong) NSArray *headerArray;
/**
 *  表头语句
 */
@property(nonatomic, copy) NSString *table_header;
/**
 *  填充内容的语句
 */
@property(nonatomic, copy) NSString *table_content;
/**
 *  更新数据库的语句
 */
@property(nonatomic, copy) NSString *table_update;
@end

@implementation ESTProvinceFMDB
+ (instancetype)sharedFMDBTool {
    static ESTProvinceFMDB *instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken,^{
        instance = [[ESTProvinceFMDB alloc] init];
        [instance createDataBaseWithSql:[NSString stringWithFormat:@"CREATE TABLE %@ %@", instance.table_name, instance.table_header] tableName:instance.table_name];
    });
    return instance;
}
- (void)writeOneselfDataWithParameter:(NSDictionary *)parameter {
    if ([parameter isKindOfClass:[NSDictionary class]]) {
        NSString *sql = [NSString stringWithFormat:@"INSERT INTO %@ %@ VALUES %@;", self.table_name, self.table_header, self.table_content];
        [self.dataBase executeUpdate:sql,
         [parameter objectForKey:self.headerArray[0]],
         [parameter objectForKey:self.headerArray[1]],
         [parameter objectForKey:self.headerArray[2]],
         [parameter objectForKey:self.headerArray[3]],
         [parameter objectForKey:self.headerArray[4]],
         [parameter objectForKey:self.headerArray[5]],
         [parameter objectForKey:self.headerArray[6]],
         [parameter objectForKey:self.headerArray[7]],
         [parameter objectForKey:self.headerArray[8]],
         [parameter objectForKey:self.headerArray[9]],
         [parameter objectForKey:self.headerArray[10]],
         [parameter objectForKey:self.headerArray[11]],
         [parameter objectForKey:self.headerArray[12]],
         [parameter objectForKey:self.headerArray[13]],
         [parameter objectForKey:self.headerArray[14]],
         [parameter objectForKey:self.headerArray[15]],
         [parameter objectForKey:self.headerArray[16]]];
    }
}
// 插入多条数据
- (void)insertWithArray:(NSArray<ESTProvinceFMDB *> *)modelArray {
    if ([self.dataBase open]) {
        [modelArray enumerateObjectsUsingBlock:^(ESTProvinceFMDB * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [self insertCodeWithModel:obj];
        }];
        [self.dataBase close];
    }
}
// 插入一条数据(插入前先查询，如果有则删除再插入)
- (void)insertWithModel:(ESTProvinceFMDB *)model {
    if ([self.dataBase open]) {
        [self insertCodeWithModel:model];
        [self.dataBase close];
    }
}
- (void)insertCodeWithModel:(ESTProvinceFMDB *)model {
    // 1.执行查询语句
    FMResultSet *resultSet = [self.dataBase executeQuery:[NSString stringWithFormat:@"SELECT * FROM %@", self.table_name]];
    // 2.遍历结果
    while ([resultSet next]) {
        NSString *string = [resultSet stringForColumn:self.headerArray.lastObject];
        if ([string isEqualToString:model.PROVINCE_ID]) {
            [self deleteCodeWithMainId:model.PROVINCE_ID];
        }
    }
    NSString *sql = [NSString stringWithFormat:@"INSERT INTO %@ %@ VALUES %@;", self.table_name, self.table_header, self.table_content];
    NSDictionary *parameter = [model mj_keyValues];
    BOOL result = [self.dataBase executeUpdate:sql,
                   [parameter objectForKey:self.headerArray[0]],
                   [parameter objectForKey:self.headerArray[1]],
                   [parameter objectForKey:self.headerArray[2]],
                   [parameter objectForKey:self.headerArray[3]],
                   [parameter objectForKey:self.headerArray[4]],
                   [parameter objectForKey:self.headerArray[5]],
                   [parameter objectForKey:self.headerArray[6]],
                   [parameter objectForKey:self.headerArray[7]],
                   [parameter objectForKey:self.headerArray[8]],
                   [parameter objectForKey:self.headerArray[9]],
                   [parameter objectForKey:self.headerArray[10]],
                   [parameter objectForKey:self.headerArray[11]],
                   [parameter objectForKey:self.headerArray[12]],
                   [parameter objectForKey:self.headerArray[13]],
                   [parameter objectForKey:self.headerArray[14]],
                   [parameter objectForKey:self.headerArray[15]],
                   [parameter objectForKey:self.headerArray[16]]];
    if (result) {
        NSLog(@"插入成功");
    } else {
        NSLog(@"插入错误");
    }
}
// 删除所有数据
- (void)delete {
    if ([self.dataBase open]) {
        [self.dataBase executeUpdate:[NSString stringWithFormat:@"DROP TABLE IF EXISTS %@;", self.table_name]];
        [self.dataBase executeUpdate:[NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS %@ %@;", self.table_name, self.table_header]];
        [self.dataBase close];
    }
}
// 删除具体的省份ID的数据
- (void)deleteWithMainId:(NSString *)mainId {
    if ([self.dataBase open]) {
        [self deleteCodeWithMainId:mainId];
        [self.dataBase close];
    }
}
// 根据省份ID删除数据
- (void)deleteCodeWithMainId:(NSString *)mainId {
    NSString *sql = [NSString stringWithFormat:@"DELETE FROM %@ WHERE %@ = ?", self.table_name, self.headerArray.lastObject];
    if ([self.dataBase executeUpdate:sql, mainId]) {
        NSLog(@"删除成功");
    } else {
        NSLog(@"删除失败");
    }
}
// 根据模型的的省份ID更新数据
- (void)updateDataWithModel:(ESTProvinceFMDB *)model {
    if ([self.dataBase open]) {
        NSString *sql = [NSString stringWithFormat:@"UPDATE %@ SET %@", self.table_name, self.table_update];
        NSDictionary *parameter = [model mj_keyValues];
        BOOL result = [self.dataBase executeUpdate:sql,
                       [parameter objectForKey:self.headerArray[0]],
                       [parameter objectForKey:self.headerArray[1]],
                       [parameter objectForKey:self.headerArray[2]],
                       [parameter objectForKey:self.headerArray[3]],
                       [parameter objectForKey:self.headerArray[4]],
                       [parameter objectForKey:self.headerArray[5]],
                       [parameter objectForKey:self.headerArray[6]],
                       [parameter objectForKey:self.headerArray[7]],
                       [parameter objectForKey:self.headerArray[8]],
                       [parameter objectForKey:self.headerArray[9]],
                       [parameter objectForKey:self.headerArray[10]],
                       [parameter objectForKey:self.headerArray[11]],
                       [parameter objectForKey:self.headerArray[12]],
                       [parameter objectForKey:self.headerArray[13]],
                       [parameter objectForKey:self.headerArray[14]],
                       [parameter objectForKey:self.headerArray[15]],
                       [parameter objectForKey:self.headerArray[16]]];
        if (result) {
            NSLog(@"更新成功");
        } else {
            NSLog(@"更新失败");
        }
        [self.dataBase close];
    }
}
// 根据省份ID查询整个模型
- (ESTProvinceFMDB *)queryWithMainId:(NSString *)mainId {
    ESTProvinceFMDB *model = [[ESTProvinceFMDB alloc] init];
    BOOL isTure = NO;
    if ([self.dataBase open]) {
        // 1.执行查询语句
        FMResultSet *resultSet = [self.dataBase executeQuery:[NSString stringWithFormat:@"SELECT * FROM %@", self.table_name]];
        // 2.遍历结果
        while ([resultSet next]) {
            NSString *string = [resultSet stringForColumn:self.headerArray.lastObject];
            if ([string isEqualToString:mainId]) {
                isTure = YES;
                __block NSDictionary *dictM = [NSMutableDictionary dictionaryWithCapacity:self.headerArray.count];
                [self.headerArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    [dictM setValue:[resultSet stringForColumn:obj] forKey:obj];
                }];
                model = [ESTProvinceFMDB mj_objectWithKeyValues:dictM];
            }
        }
        [self.dataBase close];
    }
    if (isTure) {
        return model;
    }
    return nil;
}
// 查询所有
- (NSArray<ESTProvinceFMDB *> *)query {
    NSMutableArray *arrayM = [NSMutableArray array];
    if ([self.dataBase open]) {
        // 1.执行查询语句
        FMResultSet *resultSet = [self.dataBase executeQuery:[NSString stringWithFormat:@"SELECT * FROM %@", self.table_name]];
        // 2.遍历结果
        while ([resultSet next]) {
            __block NSDictionary *dictM = [NSMutableDictionary dictionaryWithCapacity:self.headerArray.count];
            [self.headerArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                [dictM setValue:[resultSet stringForColumn:obj] forKey:obj];
            }];
            ESTProvinceFMDB *model = [ESTProvinceFMDB mj_objectWithKeyValues:dictM];
            [arrayM addObject:model];
        }
        [self.dataBase close];
    }
    return [arrayM mutableCopy];
}
// 查询最后修改的时间
- (NSString *)lastModifyWithLicenseId:(NSString *)licenseId {
    NSString *MODIFY_DATE = @"";
    if ([self.dataBase open]) {
        FMResultSet *resultSet = [self.dataBase executeQuery:[NSString stringWithFormat:@"SELECT * FROM %@", self.table_name]];
        // 2.遍历结果
        while ([resultSet next]) {
            NSString *CACHE_FLAG = [resultSet stringForColumn:@"CACHE_FLAG"];
            NSString *LICENSE_ID = [resultSet stringForColumn:@"LICENSE_ID"];
            if ([CACHE_FLAG isEqualToString:@"1"] && [LICENSE_ID isEqualToString:licenseId]) {
                MODIFY_DATE = [resultSet stringForColumn:@"MODIFY_DATE"];
                break;
            }
        }
    }
    [self.dataBase close];
    return MODIFY_DATE;
}
#pragma mark - get方法
- (NSString *)table_name {
    if (_table_name == nil) {
        _table_name = Table_Name_Province;
    }
    return _table_name;
}
- (NSArray *)headerArray {
    if (_headerArray == nil) {
        _headerArray = @[@"PROVINCE_ID",
                         @"LINK_ID",
                         @"LICENSE_ID",
                         @"PROVINCE_CODE",
                         @"PROVINCE_NAME",
                         @"WHOLE_NAME",
                         @"PRE_PINYIN",
                         @"PINYIN",
                         @"INITIAL",
                         @"LON",
                         @"LAT",
                         @"REMARK",
                         @"CACHE_FLAG",
                         @"LAST_CACHE_DATE",
                         @"CREATE_DATE",
                         @"MODIFY_DATE",
                         @"UPDATE_CONTROL_ID"
                         ];
    }
    return _headerArray;
}
- (NSString *)table_header {
    if (_table_header == nil) {
        NSString *str = [@"(" stringByAppendingString:self.headerArray.firstObject];
        for (NSInteger i = 1; i < self.headerArray.count; i++) {
            str = [NSString stringWithFormat:@"%@, %@", str, self.headerArray[i]];
        }
        str = [str stringByAppendingString:@")"];
        _table_header = str;
    }
    return _table_header;
}
- (NSString *)table_content {
    if (_table_content == nil) {
        NSString *str = @"(?";
        for (NSInteger i = 1; i < self.headerArray.count; i++) {
            str = [NSString stringWithFormat:@"%@, ?", str];
        }
        str = [str stringByAppendingString:@")"];
        _table_content = str;
    }
    return _table_content;
}
- (NSString *)table_update {
    if (_table_update == nil) {
        NSString *str = [NSString stringWithFormat:@"(%@ = ?", self.headerArray.firstObject];
        for (NSInteger i = 1; i < self.headerArray.count - 1; i++) {
            str = [NSString stringWithFormat:@"%@ %@= ?", str, self.headerArray[i]];
        }
        str = [NSString stringWithFormat:@"%@ WHERE %@ = ?)", str, self.headerArray.lastObject];
        _table_update = str;
    }
    return _table_update;
}
@end

@interface ESTCityFMDB ()
/**
 *  表
 */
@property(nonatomic, copy) NSString *table_name;
/**
 *  表头项目
 */
@property(nonatomic, strong) NSArray *headerArray;
/**
 *  表头语句
 */
@property(nonatomic, copy) NSString *table_header;
/**
 *  填充内容的语句
 */
@property(nonatomic, copy) NSString *table_content;
/**
 *  更新数据库的语句
 */
@property(nonatomic, copy) NSString *table_update;
@end

@implementation ESTCityFMDB
+ (instancetype)sharedFMDBTool {
    static ESTCityFMDB *instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken,^{
        instance = [[ESTCityFMDB alloc] init];
        [instance createDataBaseWithSql:[NSString stringWithFormat:@"CREATE TABLE %@ %@", instance.table_name, instance.table_header] tableName:instance.table_name];
    });
    return instance;
}
- (void)writeOneselfDataWithParameter:(NSDictionary *)parameter {
    if ([parameter isKindOfClass:[NSDictionary class]]) {
        NSString *sql = [NSString stringWithFormat:@"INSERT INTO %@ %@ VALUES %@;", self.table_name, self.table_header, self.table_content];
        [self.dataBase executeUpdate:sql,
         [parameter objectForKey:self.headerArray[0]],
         [parameter objectForKey:self.headerArray[1]],
         [parameter objectForKey:self.headerArray[2]],
         [parameter objectForKey:self.headerArray[3]],
         [parameter objectForKey:self.headerArray[4]],
         [parameter objectForKey:self.headerArray[5]],
         [parameter objectForKey:self.headerArray[6]],
         [parameter objectForKey:self.headerArray[7]],
         [parameter objectForKey:self.headerArray[8]],
         [parameter objectForKey:self.headerArray[9]],
         [parameter objectForKey:self.headerArray[10]],
         [parameter objectForKey:self.headerArray[11]],
         [parameter objectForKey:self.headerArray[12]],
         [parameter objectForKey:self.headerArray[13]],
         [parameter objectForKey:self.headerArray[14]],
         [parameter objectForKey:self.headerArray[15]],
         [parameter objectForKey:self.headerArray[16]],
         [parameter objectForKey:self.headerArray[17]],
         [parameter objectForKey:self.headerArray[18]]];
        [self.dataBase close];
    }
}
// 插入多条数据
- (void)insertWithArray:(NSArray<ESTCityFMDB *> *)modelArray {
    if ([self.dataBase open]) {
        [modelArray enumerateObjectsUsingBlock:^(ESTCityFMDB * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [self insertCodeWithModel:obj];
        }];
        [self.dataBase close];
    }
}
// 插入一条数据(插入前先查询，如果有则删除再插入)
- (void)insertWithModel:(ESTCityFMDB *)model {
    if ([self.dataBase open]) {
        [self insertCodeWithModel:model];
        [self.dataBase close];
    }
}
- (void)insertCodeWithModel:(ESTCityFMDB *)model {
    // 1.执行查询语句
    FMResultSet *resultSet = [self.dataBase executeQuery:[NSString stringWithFormat:@"SELECT * FROM %@", self.table_name]];
    // 2.遍历结果
    while ([resultSet next]) {
        NSString *string = [resultSet stringForColumn:self.headerArray.lastObject];
        if ([string isEqualToString:model.CITY_ID]) {
            [self deleteCodeWithMainId:model.CITY_ID];
        }
    }
    NSString *sql = [NSString stringWithFormat:@"INSERT INTO %@ %@ VALUES %@;", self.table_name, self.table_header, self.table_content];
    NSDictionary *parameter = [model mj_keyValues];
    BOOL result = [self.dataBase executeUpdate:sql,
                   [parameter objectForKey:self.headerArray[0]],
                   [parameter objectForKey:self.headerArray[1]],
                   [parameter objectForKey:self.headerArray[2]],
                   [parameter objectForKey:self.headerArray[3]],
                   [parameter objectForKey:self.headerArray[4]],
                   [parameter objectForKey:self.headerArray[5]],
                   [parameter objectForKey:self.headerArray[6]],
                   [parameter objectForKey:self.headerArray[7]],
                   [parameter objectForKey:self.headerArray[8]],
                   [parameter objectForKey:self.headerArray[9]],
                   [parameter objectForKey:self.headerArray[10]],
                   [parameter objectForKey:self.headerArray[11]],
                   [parameter objectForKey:self.headerArray[12]],
                   [parameter objectForKey:self.headerArray[13]],
                   [parameter objectForKey:self.headerArray[14]],
                   [parameter objectForKey:self.headerArray[15]],
                   [parameter objectForKey:self.headerArray[16]],
                   [parameter objectForKey:self.headerArray[17]],
                   [parameter objectForKey:self.headerArray[18]]];
    if (result) {
        NSLog(@"插入成功");
    } else {
        NSLog(@"插入错误");
    }
}
// 删除所有数据
- (void)delete {
    if ([self.dataBase open]) {
        [self.dataBase executeUpdate:[NSString stringWithFormat:@"DROP TABLE IF EXISTS %@;", self.table_name]];
        [self.dataBase executeUpdate:[NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS %@ %@;", self.table_name, self.table_header]];
        [self.dataBase close];
    }
}
// 删除具体的城市ID的数据
- (void)deleteWithProvinceId:(NSString *)mainId {
    if ([self.dataBase open]) {
        [self deleteCodeWithMainId:mainId];
        [self.dataBase close];
    }
}
// 根据城市ID删除数据
- (void)deleteCodeWithMainId:(NSString *)mainId {
    NSString *sql = [NSString stringWithFormat:@"DELETE FROM %@ WHERE %@ = ?", self.table_name, self.headerArray.lastObject];
    if ([self.dataBase executeUpdate:sql, mainId]) {
        NSLog(@"删除成功");
    } else {
        NSLog(@"删除失败");
    }
}
// 根据模型的的城市ID更新数据
- (void)updateDataWithModel:(ESTCityFMDB *)model {
    if ([self.dataBase open]) {
        NSString *sql = [NSString stringWithFormat:@"UPDATE %@ SET %@", self.table_name, self.table_update];
        NSDictionary *parameter = [model mj_keyValues];
        BOOL result = [self.dataBase executeUpdate:sql,
                       [parameter objectForKey:self.headerArray[0]],
                       [parameter objectForKey:self.headerArray[1]],
                       [parameter objectForKey:self.headerArray[2]],
                       [parameter objectForKey:self.headerArray[3]],
                       [parameter objectForKey:self.headerArray[4]],
                       [parameter objectForKey:self.headerArray[5]],
                       [parameter objectForKey:self.headerArray[6]],
                       [parameter objectForKey:self.headerArray[7]],
                       [parameter objectForKey:self.headerArray[8]],
                       [parameter objectForKey:self.headerArray[9]],
                       [parameter objectForKey:self.headerArray[10]],
                       [parameter objectForKey:self.headerArray[11]],
                       [parameter objectForKey:self.headerArray[12]],
                       [parameter objectForKey:self.headerArray[13]],
                       [parameter objectForKey:self.headerArray[14]],
                       [parameter objectForKey:self.headerArray[15]],
                       [parameter objectForKey:self.headerArray[16]],
                       [parameter objectForKey:self.headerArray[17]],
                       [parameter objectForKey:self.headerArray[18]]];
        if (result) {
            NSLog(@"更新成功");
        } else {
            NSLog(@"更新失败");
        }
        [self.dataBase close];
    }
}
// 根据城市ID查询整个模型
- (ESTCityFMDB *)queryWithMainId:(NSString *)mainId {
    ESTCityFMDB *model = [[ESTCityFMDB alloc] init];
    BOOL isTure = NO;
    if ([self.dataBase open]) {
        // 1.执行查询语句
        FMResultSet *resultSet = [self.dataBase executeQuery:[NSString stringWithFormat:@"SELECT * FROM %@", self.table_name]];
        // 2.遍历结果
        while ([resultSet next]) {
            NSString *string = [resultSet stringForColumn:self.headerArray.lastObject];
            if ([string isEqualToString:mainId]) {
                isTure = YES;
                __block NSDictionary *dictM = [NSMutableDictionary dictionaryWithCapacity:self.headerArray.count];
                [self.headerArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    [dictM setValue:[resultSet stringForColumn:obj] forKey:obj];
                }];
                model = [ESTCityFMDB mj_objectWithKeyValues:dictM];
            }
        }
        [self.dataBase close];
    }
    if (isTure) {
        return model;
    }
    return nil;
}
// 查询所有
- (NSArray<ESTCityFMDB *> *)query {
    NSMutableArray *arrayM = [NSMutableArray array];
    if ([self.dataBase open]) {
        // 1.执行查询语句
        FMResultSet *resultSet = [self.dataBase executeQuery:[NSString stringWithFormat:@"SELECT * FROM %@", self.table_name]];
        // 2.遍历结果
        while ([resultSet next]) {
            __block NSDictionary *dictM = [NSMutableDictionary dictionaryWithCapacity:self.headerArray.count];
            [self.headerArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                [dictM setValue:[resultSet stringForColumn:obj] forKey:obj];
            }];
            ESTCityFMDB *model = [ESTCityFMDB mj_objectWithKeyValues:dictM];
            [arrayM addObject:model];
        }
        [self.dataBase close];
    }
    return [arrayM mutableCopy];
}
// 查询最后修改的时间
- (NSString *)lastModifyWithLicenseId:(NSString *)licenseId {
    NSString *MODIFY_DATE = @"";
    if ([self.dataBase open]) {
        FMResultSet *resultSet = [self.dataBase executeQuery:[NSString stringWithFormat:@"SELECT * FROM %@", self.table_name]];
        // 2.遍历结果
        while ([resultSet next]) {
            NSString *CACHE_FLAG = [resultSet stringForColumn:@"CACHE_FLAG"];
            NSString *LICENSE_ID = [resultSet stringForColumn:@"LICENSE_ID"];
            if ([CACHE_FLAG isEqualToString:@"1"] && [LICENSE_ID isEqualToString:licenseId]) {
                MODIFY_DATE = [resultSet stringForColumn:@"MODIFY_DATE"];
                break;
            }
        }
    }
    [self.dataBase close];
    return MODIFY_DATE;
}
#pragma mark - get方法
- (NSString *)table_name {
    if (_table_name == nil) {
        _table_name = Table_Name_City;
    }
    return _table_name;
}
- (NSArray *)headerArray {
    if (_headerArray == nil) {
        _headerArray = @[
                         @"CITY_ID",
                         @"LINK_ID",
                         @"LICENSE_ID",
                         @"PROVINCE_ID",
                         @"CITY_CODE",
                         @"CITY_NAME",
                         @"WHOLE_NAME",
                         @"ZIP_CODE",
                         @"PRE_PINYIN",
                         @"PINYIN",
                         @"INITIAL",
                         @"LON",
                         @"LAT",
                         @"REMARK",
                         @"CACHE_FLAG",
                         @"LAST_CACHE_DATE",
                         @"CREATE_DATE",
                         @"MODIFY_DATE",
                         @"UPDATE_CONTROL_ID"
                         ];
    }
    return _headerArray;
}
- (NSString *)table_header {
    if (_table_header == nil) {
        NSString *str = [@"(" stringByAppendingString:self.headerArray.firstObject];
        for (NSInteger i = 1; i < self.headerArray.count; i++) {
            str = [NSString stringWithFormat:@"%@, %@", str, self.headerArray[i]];
        }
        str = [str stringByAppendingString:@")"];
        _table_header = str;
    }
    return _table_header;
}
- (NSString *)table_content {
    if (_table_content == nil) {
        NSString *str = @"(?";
        for (NSInteger i = 1; i < self.headerArray.count; i++) {
            str = [NSString stringWithFormat:@"%@, ?", str];
        }
        str = [str stringByAppendingString:@")"];
        _table_content = str;
    }
    return _table_content;
}
- (NSString *)table_update {
    if (_table_update == nil) {
        NSString *str = [NSString stringWithFormat:@"(%@ = ?", self.headerArray[1]];
        for (NSInteger i = 2; i < self.headerArray.count - 1; i++) {
            str = [NSString stringWithFormat:@"%@ %@= ?", str, self.headerArray[i]];
        }
        str = [NSString stringWithFormat:@"%@ WHERE %@ = ?)", str, self.headerArray.firstObject];
        _table_update = str;
    }
    return _table_update;
}
@end

@interface ESTCountryFMDB ()
/**
 *  表
 */
@property(nonatomic, copy) NSString *table_name;
/**
 *  表头项目
 */
@property(nonatomic, strong) NSArray *headerArray;
/**
 *  表头语句
 */
@property(nonatomic, copy) NSString *table_header;
/**
 *  填充内容的语句
 */
@property(nonatomic, copy) NSString *table_content;
/**
 *  更新数据库的语句
 */
@property(nonatomic, copy) NSString *table_update;
@end

@implementation ESTCountryFMDB
+ (instancetype)sharedFMDBTool {
    static ESTCountryFMDB *instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken,^{
        instance = [[ESTCountryFMDB alloc] init];
        [instance createDataBaseWithSql:[NSString stringWithFormat:@"CREATE TABLE %@ %@", instance.table_name, instance.table_header] tableName:instance.table_name];
    });
    return instance;
}
- (void)writeOneselfDataWithParameter:(NSDictionary *)parameter {
    if ([parameter isKindOfClass:[NSDictionary class]]) {
        NSString *sql = [NSString stringWithFormat:@"INSERT INTO %@ %@ VALUES %@;", self.table_name, self.table_header, self.table_content];
        [self.dataBase executeUpdate:sql,
         [parameter objectForKey:self.headerArray[0]],
         [parameter objectForKey:self.headerArray[1]],
         [parameter objectForKey:self.headerArray[2]],
         [parameter objectForKey:self.headerArray[3]],
         [parameter objectForKey:self.headerArray[4]],
         [parameter objectForKey:self.headerArray[5]],
         [parameter objectForKey:self.headerArray[6]],
         [parameter objectForKey:self.headerArray[7]],
         [parameter objectForKey:self.headerArray[8]],
         [parameter objectForKey:self.headerArray[9]],
         [parameter objectForKey:self.headerArray[10]],
         [parameter objectForKey:self.headerArray[11]],
         [parameter objectForKey:self.headerArray[12]],
         [parameter objectForKey:self.headerArray[13]],
         [parameter objectForKey:self.headerArray[14]],
         [parameter objectForKey:self.headerArray[15]],
         [parameter objectForKey:self.headerArray[16]],
         [parameter objectForKey:self.headerArray[17]],
         [parameter objectForKey:self.headerArray[18]],
         [parameter objectForKey:self.headerArray[19]]];
        [self.dataBase close];
    }
}
// 插入多条数据
- (void)insertWithArray:(NSArray<ESTCountryFMDB *> *)modelArray {
    if ([self.dataBase open]) {
        [modelArray enumerateObjectsUsingBlock:^(ESTCountryFMDB * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [self insertCodeWithModel:obj];
        }];
        [self.dataBase close];
    }
}
// 插入一条数据(插入前先查询，如果有则删除再插入)
- (void)insertWithModel:(ESTCountryFMDB *)model {
    if ([self.dataBase open]) {
        [self insertCodeWithModel:model];
        [self.dataBase close];
    }
}
- (void)insertCodeWithModel:(ESTCountryFMDB *)model {
    // 1.执行查询语句
    FMResultSet *resultSet = [self.dataBase executeQuery:[NSString stringWithFormat:@"SELECT * FROM %@", self.table_name]];
    // 2.遍历结果
    while ([resultSet next]) {
        NSString *string = [resultSet stringForColumn:self.headerArray.lastObject];
        if ([string isEqualToString:model.AREA_ID]) {
            [self deleteCodeWithMainId:model.AREA_ID];
        }
    }
    NSString *sql = [NSString stringWithFormat:@"INSERT INTO %@ %@ VALUES %@;", self.table_name, self.table_header, self.table_content];
    NSDictionary *parameter = [model mj_keyValues];
    BOOL result = [self.dataBase executeUpdate:sql,
                   [parameter objectForKey:self.headerArray[0]],
                   [parameter objectForKey:self.headerArray[1]],
                   [parameter objectForKey:self.headerArray[2]],
                   [parameter objectForKey:self.headerArray[3]],
                   [parameter objectForKey:self.headerArray[4]],
                   [parameter objectForKey:self.headerArray[5]],
                   [parameter objectForKey:self.headerArray[6]],
                   [parameter objectForKey:self.headerArray[7]],
                   [parameter objectForKey:self.headerArray[8]],
                   [parameter objectForKey:self.headerArray[9]],
                   [parameter objectForKey:self.headerArray[10]],
                   [parameter objectForKey:self.headerArray[11]],
                   [parameter objectForKey:self.headerArray[12]],
                   [parameter objectForKey:self.headerArray[13]],
                   [parameter objectForKey:self.headerArray[14]],
                   [parameter objectForKey:self.headerArray[15]],
                   [parameter objectForKey:self.headerArray[16]],
                   [parameter objectForKey:self.headerArray[17]],
                   [parameter objectForKey:self.headerArray[18]],
                   [parameter objectForKey:self.headerArray[19]]];
    if (result) {
        NSLog(@"插入成功");
    } else {
        NSLog(@"插入错误");
    }
}
// 删除所有数据
- (void)delete {
    if ([self.dataBase open]) {
        [self.dataBase executeUpdate:[NSString stringWithFormat:@"DROP TABLE IF EXISTS %@;", self.table_name]];
        [self.dataBase executeUpdate:[NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS %@ %@;", self.table_name, self.table_header]];
        [self.dataBase close];
    }
}
// 删除具体的区县ID的数据
- (void)deleteWithProvinceId:(NSString *)mainId {
    if ([self.dataBase open]) {
        [self deleteCodeWithMainId:mainId];
        [self.dataBase close];
    }
}
// 根据区县ID删除数据
- (void)deleteCodeWithMainId:(NSString *)mainId {
    NSString *sql = [NSString stringWithFormat:@"DELETE FROM %@ WHERE %@ = ?", self.table_name, self.headerArray.lastObject];
    if ([self.dataBase executeUpdate:sql, mainId]) {
        NSLog(@"删除成功");
    } else {
        NSLog(@"删除失败");
    }
}
// 根据模型的的区县ID更新数据
- (void)updateDataWithModel:(ESTCountryFMDB *)model {
    if ([self.dataBase open]) {
        NSString *sql = [NSString stringWithFormat:@"UPDATE %@ SET %@", self.table_name, self.table_update];
        NSDictionary *parameter = [model mj_keyValues];
        BOOL result = [self.dataBase executeUpdate:sql,
                       [parameter objectForKey:self.headerArray[0]],
                       [parameter objectForKey:self.headerArray[1]],
                       [parameter objectForKey:self.headerArray[2]],
                       [parameter objectForKey:self.headerArray[3]],
                       [parameter objectForKey:self.headerArray[4]],
                       [parameter objectForKey:self.headerArray[5]],
                       [parameter objectForKey:self.headerArray[6]],
                       [parameter objectForKey:self.headerArray[7]],
                       [parameter objectForKey:self.headerArray[8]],
                       [parameter objectForKey:self.headerArray[9]],
                       [parameter objectForKey:self.headerArray[10]],
                       [parameter objectForKey:self.headerArray[11]],
                       [parameter objectForKey:self.headerArray[12]],
                       [parameter objectForKey:self.headerArray[13]],
                       [parameter objectForKey:self.headerArray[14]],
                       [parameter objectForKey:self.headerArray[15]],
                       [parameter objectForKey:self.headerArray[16]],
                       [parameter objectForKey:self.headerArray[17]],
                       [parameter objectForKey:self.headerArray[18]],
                       [parameter objectForKey:self.headerArray[19]]];
        if (result) {
            NSLog(@"更新成功");
        } else {
            NSLog(@"更新失败");
        }
        [self.dataBase close];
    }
}
// 根据区县ID查询整个模型
- (ESTCountryFMDB *)queryWithMainId:(NSString *)mainId {
    ESTCountryFMDB *model = [[ESTCountryFMDB alloc] init];
    BOOL isTure = NO;
    if ([self.dataBase open]) {
        // 1.执行查询语句
        FMResultSet *resultSet = [self.dataBase executeQuery:[NSString stringWithFormat:@"SELECT * FROM %@", self.table_name]];
        // 2.遍历结果
        while ([resultSet next]) {
            NSString *string = [resultSet stringForColumn:self.headerArray.lastObject];
            if ([string isEqualToString:mainId]) {
                isTure = YES;
                __block NSDictionary *dictM = [NSMutableDictionary dictionaryWithCapacity:self.headerArray.count];
                [self.headerArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    [dictM setValue:[resultSet stringForColumn:obj] forKey:obj];
                }];
                model = [ESTCountryFMDB mj_objectWithKeyValues:dictM];
            }
        }
        [self.dataBase close];
    }
    if (isTure) {
        return model;
    }
    return nil;
}
// 查询所有
- (NSArray<ESTCountryFMDB *> *)query {
    NSMutableArray *arrayM = [NSMutableArray array];
    if ([self.dataBase open]) {
        // 1.执行查询语句
        FMResultSet *resultSet = [self.dataBase executeQuery:[NSString stringWithFormat:@"SELECT * FROM %@", self.table_name]];
        // 2.遍历结果
        while ([resultSet next]) {
            __block NSDictionary *dictM = [NSMutableDictionary dictionaryWithCapacity:self.headerArray.count];
            [self.headerArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                [dictM setValue:[resultSet stringForColumn:obj] forKey:obj];
            }];
            ESTCountryFMDB *model = [ESTCountryFMDB mj_objectWithKeyValues:dictM];
            [arrayM addObject:model];
        }
        [self.dataBase close];
    }
    return [arrayM mutableCopy];
}
// 查询最后修改的时间
- (NSString *)lastModifyWithLicenseId:(NSString *)licenseId {
    NSString *MODIFY_DATE = @"";
    if ([self.dataBase open]) {
        FMResultSet *resultSet = [self.dataBase executeQuery:[NSString stringWithFormat:@"SELECT * FROM %@", self.table_name]];
        // 2.遍历结果
        while ([resultSet next]) {
            NSString *CACHE_FLAG = [resultSet stringForColumn:@"CACHE_FLAG"];
            NSString *LICENSE_ID = [resultSet stringForColumn:@"LICENSE_ID"];
            if ([CACHE_FLAG isEqualToString:@"1"] && [LICENSE_ID isEqualToString:licenseId]) {
                MODIFY_DATE = [resultSet stringForColumn:@"MODIFY_DATE"];
                break;
            }
        }
    }
    [self.dataBase close];
    return MODIFY_DATE;
}
#pragma mark - get方法
- (NSString *)table_name {
    if (_table_name == nil) {
        _table_name = Table_Name_Country;
    }
    return _table_name;
}
- (NSArray *)headerArray {
    if (_headerArray == nil) {
        _headerArray = @[
                         @"AREA_ID",
                         @"LINK_ID",
                         @"LICENSE_ID",
                         @"PROVINCE_ID",
                         @"CITY_ID",
                         @"AREA_CODE",
                         @"AREA_NAME",
                         @"WHOLE_NAME",
                         @"ZIP_CODE",
                         @"PRE_PINYIN",
                         @"PINYIN",
                         @"INITIAL",
                         @"LON",
                         @"LAT",
                         @"REMARK",
                         @"CACHE_FLAG",
                         @"LAST_CACHE_DATE",
                         @"CREATE_DATE",
                         @"MODIFY_DATE",
                         @"UPDATE_CONTROL_ID"
                         ];
    }
    return _headerArray;
}
- (NSString *)table_header {
    if (_table_header == nil) {
        NSString *str = [@"(" stringByAppendingString:self.headerArray.firstObject];
        for (NSInteger i = 1; i < self.headerArray.count; i++) {
            str = [NSString stringWithFormat:@"%@, %@", str, self.headerArray[i]];
        }
        str = [str stringByAppendingString:@")"];
        _table_header = str;
    }
    return _table_header;
}
- (NSString *)table_content {
    if (_table_content == nil) {
        NSString *str = @"(?";
        for (NSInteger i = 1; i < self.headerArray.count; i++) {
            str = [NSString stringWithFormat:@"%@, ?", str];
        }
        str = [str stringByAppendingString:@")"];
        _table_content = str;
    }
    return _table_content;
}
- (NSString *)table_update {
    if (_table_update == nil) {
        NSString *str = [NSString stringWithFormat:@"(%@ = ?", self.headerArray[1]];
        for (NSInteger i = 2; i < self.headerArray.count - 1; i++) {
            str = [NSString stringWithFormat:@"%@ %@= ?", str, self.headerArray[i]];
        }
        str = [NSString stringWithFormat:@"%@ WHERE %@ = ?)", str, self.headerArray.firstObject];
        _table_update = str;
    }
    return _table_update;
}
@end

@interface ESTSysValueFMDB ()
/**
 *  表
 */
@property(nonatomic, copy) NSString *table_name;
/**
 *  表头项目
 */
@property(nonatomic, strong) NSArray *headerArray;
/**
 *  表头语句
 */
@property(nonatomic, copy) NSString *table_header;
/**
 *  填充内容的语句
 */
@property(nonatomic, copy) NSString *table_content;
/**
 *  更新数据库的语句
 */
@property(nonatomic, copy) NSString *table_update;
@end

@implementation ESTSysValueFMDB
+ (instancetype)sharedFMDBTool {
    static ESTSysValueFMDB *instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken,^{
        instance = [[ESTSysValueFMDB alloc] init];
        [instance createDataBaseWithSql:[NSString stringWithFormat:@"CREATE TABLE %@ %@", instance.table_name, instance.table_header] tableName:instance.table_name];
    });
    return instance;
}
- (void)writeOneselfDataWithParameter:(NSDictionary *)parameter {
    if ([parameter isKindOfClass:[NSDictionary class]]) {
        NSString *sql = [NSString stringWithFormat:@"INSERT INTO %@ %@ VALUES %@;", self.table_name, self.table_header, self.table_content];
        [self.dataBase executeUpdate:sql,
         [parameter objectForKey:self.headerArray[0]],
         [parameter objectForKey:self.headerArray[1]],
         [parameter objectForKey:self.headerArray[2]],
         [parameter objectForKey:self.headerArray[3]],
         [parameter objectForKey:self.headerArray[4]],
         [parameter objectForKey:self.headerArray[5]],
         [parameter objectForKey:self.headerArray[6]],
         [parameter objectForKey:self.headerArray[7]],
         [parameter objectForKey:self.headerArray[8]],
         [parameter objectForKey:self.headerArray[9]],
         [parameter objectForKey:self.headerArray[10]],
         [parameter objectForKey:self.headerArray[11]],
         [parameter objectForKey:self.headerArray[12]],
         [parameter objectForKey:self.headerArray[13]],
         [parameter objectForKey:self.headerArray[14]],
         [parameter objectForKey:self.headerArray[15]],
         [parameter objectForKey:self.headerArray[16]],
         [parameter objectForKey:self.headerArray[17]],
         [parameter objectForKey:self.headerArray[18]]];
    }
}
// 插入多条数据
- (void)insertWithArray:(NSArray<ESTSysValueFMDB *> *)modelArray {
    if ([self.dataBase open]) {
        [modelArray enumerateObjectsUsingBlock:^(ESTSysValueFMDB * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [self insertCodeWithModel:obj];
        }];
        [self.dataBase close];
    }
}
// 插入一条数据(插入前先查询，如果有则删除再插入)
- (void)insertWithModel:(ESTSysValueFMDB *)model {
    if ([self.dataBase open]) {
        [self insertCodeWithModel:model];
        [self.dataBase close];
    }
}
- (void)insertCodeWithModel:(ESTSysValueFMDB *)model {
    // 1.执行查询语句
    FMResultSet *resultSet = [self.dataBase executeQuery:[NSString stringWithFormat:@"SELECT * FROM %@", self.table_name]];
    // 2.遍历结果
    while ([resultSet next]) {
        NSString *string = [resultSet stringForColumn:self.headerArray.lastObject];
        if ([string isEqualToString:model.VALUE_ID]) {
            [self deleteCodeWithMainId:model.VALUE_ID];
        }
    }
    NSString *sql = [NSString stringWithFormat:@"INSERT INTO %@ %@ VALUES %@;", self.table_name, self.table_header, self.table_content];
    NSDictionary *parameter = [model mj_keyValues];
    BOOL result = [self.dataBase executeUpdate:sql,
                   [parameter objectForKey:self.headerArray[0]],
                   [parameter objectForKey:self.headerArray[1]],
                   [parameter objectForKey:self.headerArray[2]],
                   [parameter objectForKey:self.headerArray[3]],
                   [parameter objectForKey:self.headerArray[4]],
                   [parameter objectForKey:self.headerArray[5]],
                   [parameter objectForKey:self.headerArray[6]],
                   [parameter objectForKey:self.headerArray[7]],
                   [parameter objectForKey:self.headerArray[8]],
                   [parameter objectForKey:self.headerArray[9]],
                   [parameter objectForKey:self.headerArray[10]],
                   [parameter objectForKey:self.headerArray[11]],
                   [parameter objectForKey:self.headerArray[12]],
                   [parameter objectForKey:self.headerArray[13]],
                   [parameter objectForKey:self.headerArray[14]],
                   [parameter objectForKey:self.headerArray[15]],
                   [parameter objectForKey:self.headerArray[16]],
                   [parameter objectForKey:self.headerArray[17]],
                   [parameter objectForKey:self.headerArray[18]]];
    if (result) {
        NSLog(@"插入成功");
    } else {
        NSLog(@"插入错误");
    }
}
// 删除所有数据
- (void)delete {
    if ([self.dataBase open]) {
        [self.dataBase executeUpdate:[NSString stringWithFormat:@"DROP TABLE IF EXISTS %@;", self.table_name]];
        [self.dataBase executeUpdate:[NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS %@ %@;", self.table_name, self.table_header]];
        [self.dataBase close];
    }
}
// 删除具体的值列表ID的数据
- (void)deleteWithMainId:(NSString *)mainId {
    if ([self.dataBase open]) {
        [self deleteCodeWithMainId:mainId];
        [self.dataBase close];
    }
}
// 根据值列表ID删除数据
- (void)deleteCodeWithMainId:(NSString *)mainId {
    NSString *sql = [NSString stringWithFormat:@"DELETE FROM %@ WHERE %@ = ?", self.table_name, self.headerArray.lastObject];
    if ([self.dataBase executeUpdate:sql, mainId]) {
        NSLog(@"删除成功");
    } else {
        NSLog(@"删除失败");
    }
}
// 根据模型的的值列表ID更新数据
- (void)updateDataWithModel:(ESTSysValueFMDB *)model {
    if ([self.dataBase open]) {
        NSString *sql = [NSString stringWithFormat:@"UPDATE %@ SET %@", self.table_name, self.table_update];
        NSDictionary *parameter = [model mj_keyValues];
        BOOL result = [self.dataBase executeUpdate:sql,
                       [parameter objectForKey:self.headerArray[0]],
                       [parameter objectForKey:self.headerArray[1]],
                       [parameter objectForKey:self.headerArray[2]],
                       [parameter objectForKey:self.headerArray[3]],
                       [parameter objectForKey:self.headerArray[4]],
                       [parameter objectForKey:self.headerArray[5]],
                       [parameter objectForKey:self.headerArray[6]],
                       [parameter objectForKey:self.headerArray[7]],
                       [parameter objectForKey:self.headerArray[8]],
                       [parameter objectForKey:self.headerArray[9]],
                       [parameter objectForKey:self.headerArray[10]],
                       [parameter objectForKey:self.headerArray[11]],
                       [parameter objectForKey:self.headerArray[12]],
                       [parameter objectForKey:self.headerArray[13]],
                       [parameter objectForKey:self.headerArray[14]],
                       [parameter objectForKey:self.headerArray[15]],
                       [parameter objectForKey:self.headerArray[16]],
                       [parameter objectForKey:self.headerArray[17]],
                       [parameter objectForKey:self.headerArray[18]]];
        if (result) {
            NSLog(@"更新成功");
        } else {
            NSLog(@"更新失败");
        }
        [self.dataBase close];
    }
}
// 根据值列表ID查询整个模型
- (ESTSysValueFMDB *)queryWithMainId:(NSString *)mainId {
    ESTSysValueFMDB *model = [[ESTSysValueFMDB alloc] init];
    BOOL isTure = NO;
    if ([self.dataBase open]) {
        // 1.执行查询语句
        FMResultSet *resultSet = [self.dataBase executeQuery:[NSString stringWithFormat:@"SELECT * FROM %@", self.table_name]];
        // 2.遍历结果
        while ([resultSet next]) {
            NSString *string = [resultSet stringForColumn:self.headerArray.lastObject];
            if ([string isEqualToString:mainId]) {
                isTure = YES;
                __block NSDictionary *dictM = [NSMutableDictionary dictionaryWithCapacity:self.headerArray.count];
                [self.headerArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    [dictM setValue:[resultSet stringForColumn:obj] forKey:obj];
                }];
                model = [ESTSysValueFMDB mj_objectWithKeyValues:dictM];
            }
        }
        [self.dataBase close];
    }
    if (isTure) {
        return model;
    }
    return nil;
}
// 查询所有
- (NSArray<ESTSysValueFMDB *> *)query {
    NSMutableArray *arrayM = [NSMutableArray array];
    if ([self.dataBase open]) {
        // 1.执行查询语句
        FMResultSet *resultSet = [self.dataBase executeQuery:[NSString stringWithFormat:@"SELECT * FROM %@", self.table_name]];
        // 2.遍历结果
        while ([resultSet next]) {
            __block NSDictionary *dictM = [NSMutableDictionary dictionaryWithCapacity:self.headerArray.count];
            [self.headerArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                [dictM setValue:[resultSet stringForColumn:obj] forKey:obj];
            }];
            ESTSysValueFMDB *model = [ESTSysValueFMDB mj_objectWithKeyValues:dictM];
            [arrayM addObject:model];
        }
        [self.dataBase close];
    }
    return [arrayM mutableCopy];
}
// 查询最后修改的时间
- (NSString *)lastModifyWithLicenseId:(NSString *)licenseId {
    NSString *MODIFY_DATE = @"";
    if ([self.dataBase open]) {
        FMResultSet *resultSet = [self.dataBase executeQuery:[NSString stringWithFormat:@"SELECT * FROM %@", self.table_name]];
        // 2.遍历结果
        while ([resultSet next]) {
            NSString *CACHE_FLAG = [resultSet stringForColumn:@"CACHE_FLAG"];
            NSString *LICENSE_ID = [resultSet stringForColumn:@"LICENSE_ID"];
            if ([CACHE_FLAG isEqualToString:@"1"] && [LICENSE_ID isEqualToString:licenseId]) {
                MODIFY_DATE = [resultSet stringForColumn:@"MODIFY_DATE"];
                break;
            }
        }
    }
    [self.dataBase close];
    return MODIFY_DATE;
}
#pragma mark - get方法
- (NSString *)table_name {
    if (_table_name == nil) {
        _table_name = Table_Name_SysValue;
    }
    return _table_name;
}
- (NSArray *)headerArray {
    if (_headerArray == nil) {
        _headerArray = @[
                         @"VALUE_ID",
                         @"VALUE_TYPE_CODE",
                         @"VALUE_TYPE_NAME",
                         @"PARENT_VALUE_CODE",
                         @"VALUE_CODE",
                         @"VALUE_NAME",
                         @"ORDER_NO",
                         @"REMARK",
                         @"BACKUP1",
                         @"BACKUP2",
                         @"BACKUP3",
                         @"BACKUP4",
                         @"BACKUP5",
                         @"LICENSE_ID",
                         @"CACHE_FLAG",
                         @"LAST_CACHE_DATE",
                         @"CREATE_DATE",
                         @"MODIFY_DATE",
                         @"UPDATE_CONTROL_ID"
                         ];
    }
    return _headerArray;
}
- (NSString *)table_header {
    if (_table_header == nil) {
        NSString *str = [@"(" stringByAppendingString:self.headerArray.firstObject];
        for (NSInteger i = 1; i < self.headerArray.count; i++) {
            str = [NSString stringWithFormat:@"%@, %@", str, self.headerArray[i]];
        }
        str = [str stringByAppendingString:@")"];
        _table_header = str;
    }
    return _table_header;
}
- (NSString *)table_content {
    if (_table_content == nil) {
        NSString *str = @"(?";
        for (NSInteger i = 1; i < self.headerArray.count; i++) {
            str = [NSString stringWithFormat:@"%@, ?", str];
        }
        str = [str stringByAppendingString:@")"];
        _table_content = str;
    }
    return _table_content;
}
- (NSString *)table_update {
    if (_table_update == nil) {
        NSString *str = [NSString stringWithFormat:@"(%@ = ?", self.headerArray[1]];
        for (NSInteger i = 2; i < self.headerArray.count - 1; i++) {
            str = [NSString stringWithFormat:@"%@ %@= ?", str, self.headerArray[i]];
        }
        str = [NSString stringWithFormat:@"%@ WHERE %@ = ?)", str, self.headerArray.firstObject];
        _table_update = str;
    }
    return _table_update;
}
@end
